import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    searchParams: {
      status: '全部',
    },
    searchShow: false,
    pendingShow: false,
    active: 0,
    list: [{
      name: '王思',
      sex: 0,
      age: '14-20岁',
      recordId: 1,
      peferralStatus: 'Pending_Department',
      statusName: '待分配科室',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 1,
      recordId: 2,
      age: '14-20岁',
      peferralStatus: 'Confirmed',
      statusName: '待确认',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 3,
      age: '14-20岁',
      peferralStatus: 'Confirm_Passage',
      statusName: '确认通过',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 4,
      age: '14-20岁',
      peferralStatus: 'Contacted',
      statusName: '已联系',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 5,
      age: '14-20岁',
      peferralStatus: 'Already_Visited',
      statusName: '已就诊',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 6,
      age: '14-20岁',
      peferralStatus: 'Already_Hospitalized',
      statusName: '已住院',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 7,
      age: '14-20岁',
      peferralStatus: 'Discharged',
      statusName: '已出院',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }, {
      name: '王思',
      sex: 0,
      recordId: 8,
      age: '14-20岁',
      peferralStatus: 'Treatment_Over',
      statusName: '诊疗结束',
      city: '江苏省无锡市',
      phoneNumber: '12343532435',
      creatTime: '2020-03-23 12:30',
    }],  // 列表数据
    statusList: [{
      title: '待分配科室',
      value: '待分配科室',
    }, {
      title: '待确认',
      value: '待确认',
    }, {
      title: '确认通过',
      value: '确认通过',
    }, {
      title: '已联系',
      value: '已联系',
    }, {
      title: '已就诊',
      value: '已就诊',
    }, {
      title: '已住院',
      value: '已住院',
    }, {
      title: '已出院',
      value: '已出院',
    }, {
      title: '诊疗结束',
      value: '诊疗结束',
    }, {
      title: '全部',
      value: '全部',
    }],
    pageNum: 1,       // 当前页码
    pageSize: 10,     // 每页数量
    triggered: false, // 下拉刷新状态
    isLoading: false,  // 加载中状态
    noMoreData: false, // 是否无更多数据
    department: '',
  },

  onLoad() {
    this.calculateScrollHeight();
    this.loadData();
  },

  // 计算滚动区域高度
  calculateScrollHeight() {
    const systemInfo = wx.getSystemInfoSync();
    const query = wx.createSelectorQuery();
    query.select('.header').boundingClientRect(rect => {
      const scrollHeight = systemInfo.windowHeight - rect.height;
      this.setData({ scrollHeight });
    }).exec();
  },

  // 加载数据（模拟接口）
  loadData() {
    if (this.data.noMoreData || this.data.isLoading) return;
    this.setData({ isLoading: true });

    // const res = await mockApi({ 
    //   pageNum: this.data.pageNum,
    //   pageSize: this.data.pageSize,
    // });
    const res = {
      data: []
    }
    if (res.data.length === 0) {
      this.setData({ noMoreData: true });
    } else {
      this.setData({
        list: this.data.list.concat(res.data),
        pageNum: this.data.pageNum + 1
      });
    }
    this.setData({ isLoading: false, triggered: false });
  },

  // 下拉刷新
  onRefresh() {
    this.setData({ triggered: true, list: [], pageNum: 1, noMoreData: false });
    this.loadData();
  },

  // 触底加载
  onReachBottom() {
    this.loadData();
  },

  onSearchChange(e) {
    const { searchParams } = this.data;
    this.setData({
      searchParams: {
        ...searchParams,
        searchKey: e.detail
      }
    });
  },

  gotoDetailPage(e) {
    const { currentTarget: { dataset: { recordId } } } = e;
    wx.navigateTo({
      url: `/pages/messageDetail/messageDetail?&recordId=${recordId}`,
    })
  },
});